package com.huazhi.wzsnw.timer;

import com.huazhi.wzsnw.dtu.SpringUtils;
import com.huazhi.wzsnw.mapper.AlertMapper;
import com.huazhi.wzsnw.model.Alert;
import com.huazhi.wzsnw.model.Ddlined;
import com.huazhi.wzsnw.model.Lined;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author zhengch
 * @date 2021年07月16日 2:12 下午
 */
public class AlterUtil {

   private static AlertMapper alertMapper;

   static {
      alertMapper = SpringUtils.getBeen(AlertMapper.class);
   }

   //新增报警数据
   public   void addAlter(String type, String reson, Lined lined, boolean flag){
      List<Alert> list = alertMapper.selectAllBynumber(lined.getNumber());
      boolean isHas = false;
      Alert oldAlert = new Alert();
      if(flag){
         for (Alert alert:list){
            //如果已有该异常
            if(reson.equals(alert.getReason())){
               isHas = true;
               oldAlert = alert;
            }
         }
         if(!isHas){
            Alert alert = new Alert();
            alert.setTid(UUID.randomUUID().toString());
            alert.setWeidu(lined.getCun());
            alert.setJingdu(lined.getZhen());
            alert.setType(type);
            alert.setNumber(lined.getNumber());
            alert.setReason(reson);
            alert.setTime(new Date());
            alert.setState("0");
            alertMapper.insert(alert);
         }else {
            oldAlert.setState("0");
            oldAlert.setTime(new Date());
            alertMapper.updateByPrimaryKeySelective(oldAlert);
         }
      }else {
         for (Alert alert :list){
            if(reson.equals(alert.getReason())){
               alert.setState("1");
               alert.setTime(new Date());
               alertMapper.updateByPrimaryKeySelective(alert);
            }
         }
      }
   }


   public  void addDcongdaAlter(String type, String reson, Ddlined ddlined, boolean flag){
      List<Alert> list = alertMapper.selectAllBynumber(ddlined.getNumber());
      boolean isHas = false;
      Alert oldAlert = new Alert();
      if(flag){
         for (Alert alert:list){
            //如果已有该异常
            if(reson.equals(alert.getReason())){
               System.out.println("编号为"+ddlined.getNumber()+"已有该异常");
               isHas = true;
               oldAlert = alert;
            }
         }
         if(!isHas){
            Alert alert = new Alert();
            alert.setTid(UUID.randomUUID().toString());
            alert.setWeidu(ddlined.getCun());
            alert.setJingdu(ddlined.getZhen());
            alert.setType(type);
            alert.setNumber(ddlined.getNumber());
            alert.setReason(reson);
            alert.setTime(new Date());
            alert.setState("0");
            alertMapper.insert(alert);
         }else {
            oldAlert.setState("0");
            oldAlert.setTime(new Date());
            alertMapper.updateByPrimaryKeySelective(oldAlert);
         }
      }else {
         for (Alert alert :list){
            if(reson.equals(alert.getReason())){
               alert.setState("1");
               alert.setTime(new Date());
               alertMapper.updateByPrimaryKeySelective(alert);
            }
         }
      }
   }
}
